package com.laikan.reader.bookfriend.adapter.impl;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import com.laikan.reader.R;
import com.laikan.reader.bookfriend.entity.PageAutherBean;
import com.laikan.reader.bookfriend.fragment.impl.BookFragment;
import com.laikan.reader.bookfriend.fragment.impl.ShorteFragment;
import com.laikan.reader.bookfriend.utils.ImageUtils;
import com.laikan.reader.bookfriend.view.SimpleViewPagerIndicator;

import java.util.List;

public class AutherFragmentAdapter extends FragmentPagerAdapter {

    private List<Fragment> mFragments;
    private ViewPager mViewPager;
    private SimpleViewPagerIndicator mIndicator;
    private TextView mShorteButton;
    private TextView mBookButton;
    private String[] mTitles;


    public AutherFragmentAdapter(FragmentManager fm, ViewPager viewPager, SimpleViewPagerIndicator indicator) {
        super(fm);
        mViewPager = viewPager;
        mIndicator = indicator;
        initView();
        setListener();
    }


    private void initView() {
        mTitles = ImageUtils.getContext().getResources().getStringArray(R.array.auther_tab_name);
        mIndicator.setTitles(mTitles);
        mViewPager.setAdapter(this);

        mShorteButton = (TextView) mIndicator.getChildAt(0);
        mBookButton = (TextView) mIndicator.getChildAt(1);

    }

    private void setListener() {

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mIndicator.scroll(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mIndicator.setOnTabClickListener(new SimpleViewPagerIndicator.OnTabClickListener() {
            @Override
            public void onTabClick(View v) {
                int position = (int) v.getTag();
                mViewPager.setCurrentItem(position, false);//点击tab跳转到指定页面
            }
        });
    }

    public void setFragments(List fragments) {
        if (this.mFragments == null) {
            this.mFragments = fragments;
        } else {
            this.mFragments.addAll(fragments);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments == null ? null : mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments == null ? 0 : mFragments.size();
    }

    private Object obj;
    public void setBeanData(PageAutherBean pageAutherBean) {
        this.obj = pageAutherBean;
        refreshData();
    }

    public Object getBeanData(){
        return obj;
    }

    private void refreshData() {
        PageAutherBean bean = (PageAutherBean) getBeanData();

        setTabStatus(bean.data.isMyself);
        setFragmentData(bean);

    }

    private void setFragmentData(PageAutherBean bean) {
        for (Fragment fragment : mFragments){
            if (fragment instanceof ShorteFragment){
                ShorteFragment shorteFragment = (ShorteFragment) fragment;
                shorteFragment.setFragmentData(bean);
            }

            if (fragment instanceof BookFragment){
                BookFragment bookFragment = (BookFragment) fragment;
                bookFragment.setFragmentData(bean);
            }
        }
    }

    private void setTabStatus(boolean isMyself) {
        if (isMyself){
            mShorteButton.setText("我的看点");
            mBookButton.setText("我的作品");
        }else{
            mShorteButton.setText("看点");
            mBookButton.setText("作品");
        }
    }


}